const express = require("express");
const db = require("../config");

const swiperRouter = express.Router();

/* 获取所有轮播图图片 */
swiperRouter.get("/getAllimg",(req,res)=>{
    const sql = "select * from db_swiperimg where state=1";
    db.query(sql,(err,result)=>{
        if(err) return res.fail(err.message);
        if(sql.length==0) return res.fail("获取轮播图失败，请重试");
        res.send({
            status:0,
            message:"获取轮播图成功",
            data:result
        })
    })
})
/* 添加轮播图片 */
swiperRouter.post("/addImage",(req,res)=>{
const sql = "insert into db_swiperimg(imgUrl) values(?)";
db.query(sql,req.body.imgSrc,(err,result)=>{
    if(err) return res._final(err.message);
    if(result.affectedRows!==1) return res.fail("添加轮播图片失败，请重试");
    res.send({
        status:0,
        message:"添加轮播图片成功"
    })
})
});
/* 删除轮播图片 */
swiperRouter.post("/delImage",(req,res)=>{
const sql = "update db_swiperimg set state=0 where id=?";
db.query(sql,req.body.id,(err,result)=>{
    if(err) return res._final(err.message);
    if(result.affectedRows!==1) return res.fail("删除轮播图片失败，请重试");
    res.send({
        status:0,
        message:"删除轮播图片成功"
    })
})
});

module.exports = {swiperRouter};